import Enc from './enc.util';
import FormUtils from './form.util';
import DiffStr from './diffStr';
import Regex from './regex.js';
import Evt from './event.util';

/**
 * 格式化带单位的数值
 * @param data
 * @return {string}
 */
const formatNumWithUnit = function (data) {
  let preFix = [],
    subFix = [];
  let tempData = data.toString();
  let tempChart = '';
  while (tempData.length > 0) {
    tempChart = tempData.slice(0, 1);
    if (tempChart.length > 0 && isNaN(Number(tempChart))) {
      preFix.push(tempChart);
      tempData = tempData.slice(1);
      tempChart = '';
    } else {
      break;
    }
  }
  while (tempData.length > 0) {
    tempChart = tempData.slice(-1);
    if (tempChart.length > 0 && isNaN(Number(tempChart))) {
      subFix.unshift(tempChart);
      tempData = tempData.slice(0, -1);
      tempChart = '';
    } else {
      break;
    }
  }
  if (tempData.length > 0) {
    tempData = tempData.replace(/,/g, '');
    if (!isNaN(Number(tempData))) {
      tempData = Number(tempData);
    }
  }
  return preFix.join('') + tempData.toString() + subFix.join('');
};

/**
 * 根据url和title获取跳转链接iframe嵌套页面地址
 * @param url
 * @param title
 * @return {*|string}
 */
const getIFrameHttpUrlLinkStr = function (url, title) {
  title = title || '';
  url = url || '';
  let result = 'javascript:void(0);';
  if (url.indexOf('http') === 0) {
    let suffix = url.slice(-5);
    if (suffix.toLowerCase().indexOf('pdf') >= 0) {
      result =
        this.$VConfig.reportShowUrl +
        '?title=' +
        this.$VUtils.Enc.base64Encode(title) +
        '&target=' +
        this.$VUtils.Enc.base64Encode(url);
    } else {
      result = url;
    }
  }
  return result;
};

const dateFormat = (dateObj, format) => {
  let tempDateObj = new Date(dateObj);
  format = format || 'yyyy-MM-ss';
  let o = {
    'M+': tempDateObj.getMonth() + 1, //月份
    'd+': tempDateObj.getDate(), //日
    'h+': tempDateObj.getHours(), //小时
    'm+': tempDateObj.getMinutes(), //分
    's+': tempDateObj.getSeconds(), //秒
    'q+': Math.floor((tempDateObj.getMonth() + 3) / 3), //季度
    S: tempDateObj.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(format))
    format = format.replace(
      RegExp.$1,
      (tempDateObj.getFullYear() + '').substr(4 - RegExp.$1.length)
    );
  for (let k in o)
    if (new RegExp('(' + k + ')').test(format))
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
      );
  return format;
};

const isFullScreen = () => {
  return (
    document.fullScreen ||
    document.mozFullScreen ||
    document.webkitIsFullScreen ||
    document.msFullScreenElement
  );
};

// 设置元素全屏
const fullScreen = el => {
  if (!isFullScreen()) {
    (el.requestFullscreen && el.requestFullscreen()) ||
      (el.mozRequestFullScreen && el.mozRequestFullScreen()) ||
      (el.webkitRequestFullscreen && el.webkitRequestFullscreen()) ||
      (el.msRequestFullscreen && el.msRequestFullscreen());
  }
};

// 退出全屏
const exitFullScreen = () => {
  if (isFullScreen()) {
    document.exitFullscreen
      ? document.exitFullscreen()
      : document.mozCancelFullScreen
      ? document.mozCancelFullScreen()
      : document.webkitExitFullscreen
      ? document.webkitExitFullscreen()
      : document.msExitFullscreen
      ? document.msExitFullscreen()
      : '';
  }
};
/**
 * 按数组模板分割字符串
 * @param str
 * @param arr
 * @param suffix
 * @returns {[]}
 */
const splitStringByArray = (str, arr, suffix) => {
  arr = arr || [];
  let resultArr = [];
  let subText = str;
  while (subText.length > 0 && arr.length > 0) {
    let curIndex = arr.shift();
    resultArr.push(subText.slice(0, curIndex));
    subText = subText.slice(curIndex);
  }
  if (resultArr.length === 0 && subText.length > 0) {
    resultArr = [str];
  } else if (subText.length > 0) {
    resultArr[resultArr.length - 1] += suffix;
  }
  return resultArr;
};

const getSVGNodeZoomInfo = svgNode => {
  let transformStr = svgNode.getAttribute('transform') || '';
  let result = [];
  if (transformStr.length > 0) {
    let index = transformStr.indexOf('translate');
    if (index >= 0) {
      let translateStr = transformStr.slice(index + 10);
      translateStr = translateStr.slice(0, translateStr.indexOf(')'));
      if (translateStr.indexOf(',') > 0) {
        result = translateStr.split(',');
      } else {
        result = translateStr.split(/\s+/);
      }
      result[0] = Number(result[0]);
      result[1] = Number(result[1]);
    } else {
      result = [0, 0];
    }

    index = transformStr.indexOf('scale');
    if (index >= 0) {
      let scaleStr = transformStr.slice(index + 6);
      scaleStr = scaleStr.slice(0, scaleStr.indexOf(')'));
      result.push(Number(scaleStr));
    } else {
      result.push(1);
    }
  }
  return {
    x: result[0] || 0,
    y: result[1] || 0,
    k: result[2] || 1
  };
};

/**
 * 获取随机字符串
 * @param num
 * @returns {string}
 */
const randomStr = num => {
  let str = 'abcdefghijklmnopqrstuvwxyz';
  num = parseInt(num, 10);
  if (isNaN(num)) {
    num = 5;
  }
  let result = [];
  for (let i = 0; i < num; i++) {
    result.push(str.charAt(Math.floor(Math.random() * str.length)));
  }
  return result.join('');
};

export default {
  dateFormat,
  formatNumWithUnit,
  getIFrameHttpUrlLinkStr,
  isFullScreen,
  fullScreen,
  exitFullScreen,
  splitStringByArray,
  getSVGNodeZoomInfo,
  randomStr,
  ...Enc,
  ...FormUtils,
  ...DiffStr,
  Regex,
  ...Evt
};
